﻿/// <reference path="jquery-1.5.1-vsdoc.js" />
$(function () {
    $(".product-list").sortable({
        placeholder: "product-placeholder",
        handle: ".drag-vertical-gripp"
    }).disableSelection();

    $('.edit-bar .buttons .btn').click(function () {
        $(this).toggleClass('active');
    });

    $('input[type=checkbox]:not(:checked)').closest(".product-list-item").toggle();

    $("#add-product").click(function () {
        $(".new-product-edit-wrapper").slideToggle("fast");
    });

    $("#edit-product-list").click(function () {
        //$(this).toggleClass("edit-list-btn edit-list-btn-active");
        $('input[type=checkbox]:not(:checked)').closest(".product-list-item").toggle('fast');
        $(".product-header > .switch-checkbox-wrapper").toggle("fast");
        $(".product-header > .delete-btn").toggle("fast");
        $(".product-header > .manage-btn").toggle("fast");
    });

    $('.product-header > .triangle').live('click', function () {
        $(this).toggleClass("triangle-1 triangle-up-down");
        //alert($(this).closest('.product-list-item').attr('product_id'));
        $(".product-body", $(this).closest('.product-list-item')).slideToggle("fast");
    });

    $('.switch-checkbox').click(function () {
        var checked = $('input[type=checkbox]', this).is(':checked');
        $('input[type=checkbox]', this).attr('checked', (!checked ? 'checked' : null));
        $.ajax({
            url: $('input[type=checkbox]', this).attr('action')
            , data: 'IsHidden=' + checked.toString()
            , type: 'PUT'
            , success: function (html) {
            }
            , error: function (xhr) {
            }
        }
        );
    });

    $('form.ajax').submit(function (e) {
        var $form = $(this);
        if ($form.valid()) {
            e.preventDefault();
            $.ajax({
                url: $(this).attr('action')
                , data: $(this).serialize()
                , type: 'post'
                , success: function (html) {
                    $(html).prependTo('.product-list');
                    $($form).trigger('reset');
                }
                , error: function (xhr) {
                    jQuery.validator.unobtrusive.parse();
                    $($form).html(xhr.responseText);
                }
            });
            return false;
        }
    });

    $('#frmAddProduct').on('reset', function () {
        $(this).trigger('close');
    });
    $('#frmAddProduct').on('close', function () {
        $('#add-product').toggleClass('active');
        $(".new-product-edit-wrapper").slideToggle("fast");
    });

    //            $.getJSON('@Url.Action("list", "products", new { area = "api" })', null, function (data) {
    //                $('#productTemplate').tmpl(data).prependTo('.product-list');
    //            });
    //            $.getJSON('@Url.Action("list", "products")', null, function (html) {
    //                $(html).prependTo('.product-list');
    //            });
    //$('.product-list').load(UrlHelper.Action('list', 'products'));

    $('.delete-btn').live('click', function (e) {
        e.preventDefault();
        var url = $(this).attr('href');
        $product = $(this).closest('.product-list-item');
        var productId = $($product).attr('product_id');
        if (confirm('Are you sure?')) {
            $.ajax({
                url: url
                , dataType: 'json'
                , type: 'DELETE'
                , success: function (data) {
                    //$('#productTemplate').tmpl(data).prependTo('.product-list');
                    $($product).slideUp();
                }
                , error: function (xhr) {
                    //                            var data = $.parseJSON(xhr.responseText);
                    //                            $(data.ValidationMessages).each(function (index, item) {
                    //                                $('span.val-msg[for=' + item.Property + ']', $form).html(item.Errors[0]).removeClass('hidden');
                    //                            });
                }
            }
            );
        }
    });
});